package controller;

import java.util.ArrayList;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import transfer.User;
import transfer.UserRole;

/**
 *
 * @author Oleh
 */
public class AdminRequestHelper {

    public static final String adminRoleString = "admin";
    public static final String warehouseRoleString = "warehouse";
    public static final String courierRoleString = "courier";
    public static final String visibleWarehouseRoleName = "warehouse employee";
    public static final String contentParameterName = "content";
    public static final String operationParameterName = "operation";
    public static final String roleParameterName = "role";
    public static final String defaultAdminContent = "user";
    public static final String defaultWarehouseContent = "order";
    public static final String defaultCourierContent = "courier";
    public static final String endJSPName = ".jsp";
    public static final String endMenuName = "menu.jsp";
    private String role;
    private String content;
    private String operation;
    private String link;
    private String menu;
    private ArrayList nameRoles;
    private String currentNameRole;
    private ArrayList roles;
    private HttpServletRequest request;

    public AdminRequestHelper(HttpServletRequest request) {
        this.request = request;
        this.content = request.getParameter(contentParameterName);
        this.operation = request.getParameter(operationParameterName);
        String roleParameter = request.getParameter(roleParameterName);
        initializeRoles();
        HttpSession session = request.getSession();
        if (roleParameter != null) {
            if (nameRoles.contains(roleParameter)) {
                role = roleParameterToRole(roleParameter);
                session.setAttribute(SessionAttribute.ROLE, role);
            } else {
                role = roleParameterToRole((String) nameRoles.get(0));
                session.setAttribute(SessionAttribute.ROLE, role);
            }
        } else {
            role = (String) session.getAttribute(SessionAttribute.ROLE);
            if (role == null) {
                role = roleParameterToRole((String) nameRoles.get(0));
                session.setAttribute(SessionAttribute.ROLE, role);
            }
        }
        currentNameRole = roleToRoleName(role);
        menu = role + endMenuName;
        if (content != null) {
            if (operation != null) {
                link = role + "/" + content + operation + endJSPName;
            } else {
                link = role + "/" + content + endJSPName;
            }
        } else {
            content = getDefaultContent();
            link = role + "/" + content + endJSPName;
        }
    }

    private void initializeRoles() {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(SessionAttribute.USER);
        ArrayList userRoles = user.getRoles();
        roles = new ArrayList();
        for (int i = 0; i < userRoles.size(); i++) {
            UserRole currentRole = (UserRole) userRoles.get(i);
            if (!currentRole.equals(UserRole.USER)) {
                roles.add(currentRole);
            }
        }
        Collections.sort(roles);
        nameRoles = new ArrayList();
        for (int i = 0; i < roles.size(); i++) {
            UserRole currentRole = (UserRole) roles.get(i);
            if (!currentRole.equals(UserRole.WAREHOUSE_EMPLOYEE)) {
                nameRoles.add(currentRole.getNameRole());
            } else {
                nameRoles.add(visibleWarehouseRoleName);
            }
        }
    }

    private String roleParameterToRole(String roleParameter) {
        if (visibleWarehouseRoleName.equals(roleParameter)) {
            return warehouseRoleString;
        }
        return roleParameter;
    }

    private String roleToRoleName(String role) {
        if (warehouseRoleString.equals(role)) {
            return visibleWarehouseRoleName;
        }
        return role;
    }

    private String getDefaultContent() {
        if (adminRoleString.equals(role)) {
            return defaultAdminContent;
        }
        if (warehouseRoleString.equals(role)) {
            return defaultWarehouseContent;
        }
        if (courierRoleString.equals(role)) {
            return defaultCourierContent;
        }
        return null;
    }

    public String getRole() {
        return role;
    }

    public String getContent() {
        return content;
    }

    public String getOperation() {
        return operation;
    }

    public String getLink() {
        return link;
    }

    public ArrayList getNameRoles() {
        return nameRoles;
    }

    public String getMenu() {
        return menu;
    }

    public String getCurrentNameRole() {
        return currentNameRole;
    }
}
